#include    "../Controlers/CSounds.h"

MMediaContent::MMediaContent()
{
    this->audio_output = NULL;
    this->media_object = NULL;
    this->media_source = NULL;
}

MMediaContent::MMediaContent(QString url)
{
    this->media_source = new Phonon::MediaSource(url);
    this->media_object = new Phonon::MediaObject();
    this->media_object->setCurrentSource(*(this->media_source));
    this->audio_output = new Phonon::AudioOutput(Phonon::MusicCategory);
    this->path = createPath(this->media_object, this->audio_output);
    CSounds::media_playing.append(this->media_object);
    CSounds::audio_playing.append(this->audio_output);
    this->audio_output->setVolume(CSounds::sounds_volume);
    this->audio_output->setMuted(CSounds::sounds_muted);
    this->media_object->play();
    this->connect(this->media_object, SIGNAL(finished()), this, SLOT(stop_sound()));
}

MMediaContent::~MMediaContent()
{
    this->path.disconnect();

    if (this->audio_output != NULL)
        delete this->audio_output;
    if (this->media_source != NULL)
        delete this->media_source;
    if (this->media_object != NULL)
        //delete this->media_object;
        this->media_object->clear();
}

void    MMediaContent::stop_sound()
{
    this->media_object->stop();
    if (CSounds::background_playing != NULL)
    {
        if (CSounds::background_playing->media_object == this->media_object)
            CSounds::background_playing = NULL;
    }

    CSounds::media_playing.removeOne(this->media_object);
    CSounds::audio_playing.removeOne(this->audio_output);
    QObject::disconnect(this);

    delete this;
}
